package com.scaffold.photo.dao;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import wangjg.commons.page.Page;

import com.scaffold.photo.entity.Album;
import com.scaffold.springmvc.dao.BasicBizDAO;

@Repository
public class AlbumDAO extends BasicBizDAO<Album, Long> {

	public List<Album> pageQuery(Page page) {
		DetachedCriteria criteria = DetachedCriteria.forClass(this.persistentClass);
		criteria.addOrder(Order.desc("id"));
		return this.findByCriteria(criteria, page);
	}

	public List<Album> pageQuery(Page page, String name, String keyword) {
		DetachedCriteria criteria = DetachedCriteria.forClass(this.persistentClass);
		if (name != null) {
			criteria.add(Restrictions.eq("name", name));
		}
		if (keyword != null && !keyword.isEmpty()) {
			criteria.add(Restrictions.like("name", keyword, MatchMode.ANYWHERE).ignoreCase());
		}
		criteria.addOrder(Order.desc("id"));
		return this.findByCriteria(criteria, page);
	}

}
